package com.hjh.demo1.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hjh.demo1.entity.Student;
import com.hjh.demo1.services.StudentService;
import com.hjh.demo1.util.Result;

@RestController
@RequestMapping("/v1/login")
public class TestController {

	protected final static Logger logger = LoggerFactory.getLogger(TestController.class);

	@Autowired
	private StudentService studentService;

	@PostMapping("/register")
	public Result Register(@RequestBody Student student) {
		studentService.insert(student);
		return new Result(Result.OK, "保存成功");
	}

	@GetMapping("/findAllStudent")
	public Result test1() {
		logger.info("test1111111111");
		Result result = new Result();
		List<Student> student = studentService.findAllStudent();
		result.setData(student);
		return result;
	}

	@GetMapping("findSomeColumn")
	public Result test2() {
		Result result = new Result();
		List<Student> stu = studentService.findSomeColumn();
		result.setData(stu);
		return result;
	}

	@GetMapping("deleteById/{id}")
	public Result test3(@PathVariable Integer id) {
		Result result = new Result();
		studentService.deleteById(id);
		result.setMsg("删除成功");
		return result;
	}

	@PostMapping("updateByPrimarKeySelective")
	public Result test4(@RequestBody Student student) {
		Result result = new Result();
		EntityWrapper<Student> entityWrapper = new EntityWrapper<>();
		entityWrapper.eq("stu_mobile", student.getStuMobile());
		Student stu = studentService.selectOne(entityWrapper);
		if (null != stu) {
			stu.setParName("my hero");
			stu.setStuName("zxs");
		}
		studentService.updateByPrimarKeySelective(stu);
		result.setData(stu);
		return result;
	}

	@PostMapping("/saveStudent")
	public Result<Student> test5(@RequestBody Student student) {
		Result<Student> result = new Result<Student>();
		studentService.saveStudent(student);
		result.setData(student);

		return result;

	}

	/**
	 * 分页的方法
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@PostMapping("page/{pageNumber}")
	public Result findAllStuPage(@PathVariable Integer pageNumber, @RequestParam(defaultValue = "6") Integer pageSize) {
		Result result = new Result();
		Page page = new Page(pageNumber, pageSize);
		Page<Student> pageStu = studentService.findAllStudentPage(page);
		result.setData(pageStu.getRecords());
		return result;

	}

	@PostMapping("pageByGender/{pageNumber}")
	public Result findStuByGender(@PathVariable Integer pageNumber,
			@RequestParam(defaultValue = "6") Integer pageSize) {
		Result result = new Result<>();

		EntityWrapper<Student> wrapper = new EntityWrapper<>();
		wrapper.eq("gender", 1);
		Page<Student> page = getPage(pageNumber, pageSize);
		Page<Student> stuPage = studentService.selectPage(page, wrapper);
		result.setData(stuPage.getRecords());

		return result;
	}

	/**
	 * 获取分页对象 每页显示数量
	 */

	private <T> Page<T> getPage(int pageNum, int pageSize) {

		return new Page<T>(pageNum, pageSize);
	}

}
